package com.qyl.deguru.controller.admin;


import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qyl.deguru.common.PageConstant;
import com.qyl.deguru.common.PageModel;
import com.qyl.deguru.dto.ShopType;
import com.qyl.deguru.service.ShopTypeService;
import com.qyl.deguru.service.impl.ShopTypeServcieImple;

/**
 * 
 * 商品类型控制类
 * */
@Controller
@RequestMapping("/admin/shopType")
public class ShopTypeController {
	ShopTypeService shopTypeService = new ShopTypeServcieImple();

	// 分页显示显示商品类型
	@RequestMapping("/toShopTypeList")
	public String showShopTypeList(String pageIndex,String pageSize,ModelMap modelmap, ShopType shopType) {
		// 实例化分页
		PageModel pageModel = new PageModel();
		// 当前页为空，设置当前页=0
		if ("".equals(pageIndex) || pageIndex == null) {
			pageModel.setPageIndex(0);
		} else {
			pageModel.setPageIndex(Integer.parseInt(pageIndex));
		}
		// 判断每页显示的记录是否大于0
		if (pageSize != null && pageSize != "") {
			pageModel.setPageSize(Integer.parseInt(pageSize));
		}
		// 获取总记录数
		int recordcount = shopTypeService.getcount();
		pageModel.setRecordCount(recordcount);
		// 显示商品详细信息
		List<ShopType> shopTypes = shopTypeService.showShopTypeList(shopType,pageModel);
		// 将商品详细信息,回显数据,分页存储到缓存中
		modelmap.addAttribute("shopTypes", shopTypes);
		modelmap.addAttribute("shopType", shopType);
		modelmap.addAttribute("pageModel", pageModel);
		return "shopType/shopTypeList";
	}
	//根据id删除商品类型
	@RequestMapping("/delShopType")
	public String delShopType(String code,ModelMap modelmap){
		int count=shopTypeService.delShopType(code);
		if(count>0){
			modelmap.put(PageConstant.TIP, "删除成功");
		}else{
			modelmap.put(PageConstant.TIP, "删除失败");
		}
		return "forward:toShopTypeList";
	}
	//批量删除
	@RequestMapping("/delShopTypes")
	public String delShopTypes(String[] ids,ModelMap modelmap) {
		int count=shopTypeService.delShopTypes(ids);
		if(count>0){
			modelmap.addAttribute(PageConstant.TIP, "删除成功");
		}else{
			modelmap.addAttribute(PageConstant.TIP,"删除失败");
		}
		return "forward:toShopTypeList";
	}
	//根据id修改商品类型信息
	@RequestMapping("/toShopTypeUpdate")
	public String toShopTypeUpdate(String code,ModelMap modelmap){
		//根据code获取商品类型信息
		ShopType shopType=shopTypeService.getShopTypeByCode(code);
		modelmap.addAttribute("shopType", shopType);
		return "shopType/shopTypeUpdate";
	}
	//更新商品类型信息
	@RequestMapping("/shopTypeUpdate")
	public String shopTypeUpdate(ModelMap modelmap,ShopType shopType){
		int count=shopTypeService.updateShopType(shopType);
		if(count>0){
			modelmap.addAttribute(PageConstant.TIP, "更新成功");
		}else{
			modelmap.addAttribute(PageConstant.TIP,"更新失败");
		}
		return "shopType/shopTypeUpdate";
	}
	//跳转到新增页面
	@RequestMapping("/toAddShopType")
	public String toAddShopType() {
		return "shopType/shopTypeAdd";
	}
	//新增商品类型
	@RequestMapping("/addShopType")
	public String addShopType(ModelMap modelmap,ShopType shopType) {
		int count=shopTypeService.addShopType(shopType);
		if(count>0) {
			modelmap.addAttribute(PageConstant.TIP,"新增成功");
		}else {
			modelmap.addAttribute(PageConstant.TIP, "新增失败");
		}
		return "shopType/shopTypeAdd";
	}
	//异步获取：获取商品下拉类型
	@RequestMapping("/getShopTypelist")
	@ResponseBody
	public List<Map<String,String>> getShopTypeList() {
		List<Map<String,String>> TypeList=shopTypeService.getTypeList();
		return TypeList;
	}
}
